package com.vito.dcs.sys.sysMaterial.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.exception.ParamErrorException;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.dcs.sys.material.model.SysMaterialModel;
import com.vito.dcs.sys.material.service.SysMaterialService;

/**
 * 材料类型
 * @author lyhao
 *
 */
@Controller
@RequestMapping("/dcs/sys/material")
public final class SysMaterialAction extends BaseAction {
	
	private static final Logger log = LoggerFactory.getLogger(SysMaterialAction.class);
	
	@Reference
	private SysMaterialService sysMaterialServiceImpl;
	
	/**
	 * 保存
	 * @param sysMaterialModel
	 * @return
	 */
	@RequestMapping("saveSysMaterial")
	@ResponseBody
	public Result saveSysMaterial(@RequestBody SysMaterialModel sysMaterialModel){
		Result res = new Result();
        UserModel user = SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			Validata.isNullOrEmpty(sysMaterialModel, "材料类型表不能为空");
			Validata.isNullOrEmpty(sysMaterialModel.getLayerId(), "结构层id不能为空");
			Validata.isNullOrEmpty(sysMaterialModel.getMaterialName(), "材料类型名称不能为空");
			sysMaterialModel.setUpdateUser(user.getUserId());
			sysMaterialServiceImpl.saveSysMaterial(sysMaterialModel);
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.SAVESUC);
		}catch(ParamErrorException e){
			res.setMsg(e.getMessage());
			res.setCode(Result.CODE_ERROR);
		}catch(Exception ex){
			log.error("保存失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 分页查询
	 * @param sysMaterialModel
	 * @return
	 */
	@RequestMapping("querySysMaterialByPage")
	@ResponseBody
	public Result querySysMaterialByPage(SysMaterialModel sysMaterialModel){
		Result res = new Result();
        UserModel user =SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			res.setData(sysMaterialServiceImpl.querySysMaterialByPage(sysMaterialModel));
			res.setCode(Result.CODE_OK);
		}catch(Exception ex){
			log.error("查询失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 查询
	 * @param sysMaterialModel
	 * @return
	 */
	@RequestMapping("querySysMaterial")
	@ResponseBody
	public Result querySysMaterial(SysMaterialModel sysMaterialModel){
		Result res = new Result();
        UserModel user =SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			res.setData(sysMaterialServiceImpl.querySysMaterial(sysMaterialModel));
			res.setCode(Result.CODE_OK);
		}catch(Exception ex){
			log.error("查询失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
		
	/**
	 * 修改
	 * @param sysMaterialModel
	 * @return
	 */
	@RequestMapping("updateSysMaterial")
	@ResponseBody
	public Result updateSysMaterial(@RequestBody SysMaterialModel sysMaterialModel){
		Result res = new Result();
        UserModel user =SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			sysMaterialModel.setUpdateUser(user.getUserId());
			res.setData(sysMaterialServiceImpl.updateSysMaterial(sysMaterialModel));
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.UPDSUC);
		}catch(Exception ex){
			log.error("更新失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	/**
	 * 删除
	 * @param sysMaterialModel
	 * @return
	 */
	@RequestMapping("deleteSysMaterial")
	@ResponseBody
	public Result deleteSysMaterial(SysMaterialModel sysMaterialModel){
		Result res = new Result();
        UserModel user =SessionMap.getObj();
        if(null==user){
            res.setCode(Result.CODE_LOGIN_REQUIRE);
            res.setMsg(ResMsg.MSG_LOGIN_NEED);
            return res;
        }
		try{
			Validata.isNullOrEmpty(sysMaterialModel.getId(), "所选项不能为空");
			res.setData(sysMaterialServiceImpl.deleteSysMaterial(sysMaterialModel));
			res.setCode(Result.CODE_OK);
			res.setMsg(ResMsg.DELCOM);
		}catch(ParamErrorException e){
			res.setMsg(e.getMessage());
			res.setCode(Result.CODE_ERROR);
		}catch(Exception ex){
			log.error("删除失败",ex);
			res.setCode(Result.CODE_ERROR);
			res.setMsg(ResMsg.EXECERR);
		}
		return res;
	}
	
	@RequestMapping("queryMaterialListByLayerId")
	@ResponseBody
	public Result queryMaterialListByLayerId(@RequestParam String layerId){
		Result rs = new Result();
		try{
			rs.setData(sysMaterialServiceImpl.queryMaterialListByLayerId(layerId));
			rs.setCode(Result.CODE_OK);
		}catch(Exception ex){
			log.error("查询失败",ex);
			rs.setCode(Result.CODE_ERROR);
			rs.setMsg(ResMsg.EXECERR);
		}
		return rs;
	}
}



